<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="show">
      <span class="placeholder" v-if="!address.fullLocation">{{ fullLocation || '请选择配送地址' }}</span>
      <span class="value" v-else>{{address.fullLocation}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="isShow">
      <span class="ellipsis" @click="getSite(item)" v-for="item in list" :key="item.code">{{item.name}}</span>
    </div>
  </div>
</template>

<script>
import { computed, reactive, ref, watch } from 'vue'
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'

export default ({
  name: 'XtxCity',
  props: {
    fullLocation: {
      type: String,
      default: '请选择配送地址'
    }
  },
  setup (props, { emit }) {
    watch(() => props.fullLocation, (val) => {
      if (val === '') {
        for (const key in address) {
          address[key] = ''
        }
      } else if (address) {
        address.fullLocation = val
      }
    }, { immediate: true })
    const isShow = ref(false)
    const target = ref(null)
    onClickOutside(target, () => {
      isShow.value = false
    })
    const result = ref([])
    // 发送请求获取城市数据
    if (!window.cityData) {
      axios('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json').then(res => {
        result.value = window.cityData = res.data
      })
    } else result.value = window.cityData
    // 定义城市数据
    const address = reactive({
      // 🔔按照添加收货地址的接口文档参数要求准备数据
      // code 用于后端请求，name 是用于前端展示
      provinceCode: '', // 所在省份编码
      provinceName: '', // 所在省份名字
      cityCode: '', // 所在城市编码
      cityName: '',
      countyCode: '', // 所在区/县编码
      countyName: '',
      fullLocation: '' // 完整地址，包括省市区3级的文字
    })
    const getSite = (item) => {
      if (item.level === 0) {
        address.provinceCode = item.code
        address.provinceName = item.name
      } else if (item.level === 1) {
        address.cityCode = item.code
        address.cityName = item.name
      } else {
        address.countyCode = item.code
        address.countyName = item.name
        isShow.value = false
        address.fullLocation = address.provinceName + ' ' + address.cityName + ' ' + address.countyName
        emit('change', address)
      }
    }
    const list = computed({
      get () {
        let temp = result.value
        if (address.provinceCode) temp = (temp.find((v) => v.code === address.provinceCode)).areaList
        if (address.provinceCode && address.cityCode) temp = (temp.find((v) => v.code === address.cityCode)).areaList
        return temp
      }
    })
    const show = () => {
      isShow.value = !isShow.value
      for (const k in address) {
        address[k] = ''
      }
    }
    return { show, isShow, target, getSite, list, address }
  }
})
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
